package com.xbongbong.pro.statistic.service.impl;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.toolbox.util.MD5Util;
import com.xbongbong.pro.domain.entity.BiDataCorrEntity;
import com.xbongbong.pro.domain.entity.BiProWareEntity;
import com.xbongbong.pro.domain.entity.ChartEntity;
import com.xbongbong.pro.statistic.enums.FormLocationEnum;
import com.xbongbong.pro.statistic.help.CustomHelp;
import com.xbongbong.pro.statistic.model.BiDataCorrModel;
import com.xbongbong.pro.statistic.model.BiProWareModel;
import com.xbongbong.pro.statistic.pojo.SlaveSourcePojo;
import com.xbongbong.pro.statistic.service.BiProWareService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 梁鲁江
 * @version v1.0
 * @date 2020/5/14 14:59
 * @since v1.0
 */
@Service("biProWareService")
public class BiProWareServiceImpl implements BiProWareService {
    @Resource
    private BiProWareModel biProWareModel;
    @Resource
    private CustomHelp customHelp;
    @Override
    public List<BiProWareEntity> getBiProWareEntities(ChartEntity chartEntity) {
        String tableSerial = getTableSerial(chartEntity);
        return getBiProWareEntities(chartEntity.getCorpid(), tableSerial);
    }

    @Override
    public List<BiProWareEntity> getBiProWareEntities(String corpid, String tableSerial) {
        Map<String, Object> wparam = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        wparam.put("corpid", corpid);
        wparam.put("tableSerial",tableSerial);
        return biProWareModel.findEntitys(wparam);
    }

    @Override
    public String getTableSerial(ChartEntity chartEntity) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(chartEntity.getDriverSources().getLong("formId")+"_"+chartEntity.getDriverSources().getString("linkAttrCopy"));
        customHelp.stringAppend(stringBuilder, chartEntity.getDriverSources().getString("linkAttrBCopy"));
        customHelp.stringAppend(stringBuilder, chartEntity.getDriverSources().getString("linkAttrCCopy"));
        customHelp.stringAppend(stringBuilder, chartEntity.getDriverSources().getString("linkAttrDCopy"));
        customHelp.stringAppend(stringBuilder, chartEntity.getDriverSources().getString("linkAttrECopy"));
        List<SlaveSourcePojo> slaveSourceList = chartEntity.getSlaveSources().toJavaList(SlaveSourcePojo.class);
        for (SlaveSourcePojo slaveSourcePojo : slaveSourceList) {
            stringBuilder.append("_"+slaveSourcePojo.getFormId()+"_"+slaveSourcePojo.getLinkAttrCopy());
        }
        //把映射字段也加进来
        stringBuilder.append("_"+chartEntity.getFormIdAttr());
        return MD5Util.EncoderByMd5(stringBuilder.toString());
    }
    @Override
    public String getTableSerial(List<BiDataCorrEntity> allCorrelationList,ChartEntity chartEntity) {
        StringBuilder stringBuilder = new StringBuilder();
        for (BiDataCorrEntity biDataCorrEntity : allCorrelationList) {
            if(biDataCorrEntity.getLocation() == 1){
                stringBuilder.append(biDataCorrEntity.getFormId()+"_"+biDataCorrEntity.getLinkAttr().trim());
                //不确定后面的关联字段是否存在
                customHelp.stringAppend(stringBuilder, biDataCorrEntity.getLinkAttrB());
                customHelp.stringAppend(stringBuilder, biDataCorrEntity.getLinkAttrC());
                customHelp.stringAppend(stringBuilder, biDataCorrEntity.getLinkAttrD());
                customHelp.stringAppend(stringBuilder, biDataCorrEntity.getLinkAttrE());
            }else {
                stringBuilder.append("_"+biDataCorrEntity.getFormId()+"_"+biDataCorrEntity.getLinkAttr().trim());
            }
        }
        //把映射字段也加进来
        stringBuilder.append("_"+chartEntity.getFormIdAttr());
        return MD5Util.EncoderByMd5(stringBuilder.toString());
    }
}
